﻿using System.Text;
using System.Web.Routing;
using SlamCms.Common;
using SlamCms.Data;
using SlamCms.SharePointServices;
using SlamCms.Web;

namespace System.Web.Mvc
{
	public static class SlamCmsUrlHelperExtensions
	{
		public static string AbsoluteAction(this UrlHelper urlHelper, string actionName, string controllerName, object routeValues)
		{
			return urlHelper.AbsoluteAction(actionName, controllerName, new RouteValueDictionary(routeValues));
		}

		public static string AbsoluteAction(this UrlHelper urlHelper, string actionName, string controllerName, RouteValueDictionary routeValues)
		{
			var relativeUrl = urlHelper.Action(actionName, controllerName, routeValues);
			var uriService = DependencyResolver.Current.GetService<IBaseUriResolver>();
			var absoluteUri = new Uri(uriService.GetBaseUri(), new Uri(relativeUrl, UriKind.Relative));
			return absoluteUri.ToString();
		}

		public static string GetReturnUrl(this UrlHelper helper)
		{
			return GetReturnUrl(helper.RequestContext.HttpContext);
		}

		public static string GetReturnUrl(this ViewPage pg)
		{
			return GetReturnUrl(pg.ViewContext.HttpContext);
		}

		public static string GetReturnUrl(this ViewMasterPage pg)
		{
			return GetReturnUrl(pg.Page as ViewPage);
		}

		public static string GetReturnUrl(this ViewUserControl pg)
		{
			return GetReturnUrl(pg.Page as ViewPage);
		}

		public static string GetReturnUrl(HttpContextBase context)
		{
			var returnUrl = "";

			if (context.Request.QueryString["ReturnUrl"] != null)
			{
				returnUrl = context.Request.QueryString["ReturnUrl"];
			}

			return returnUrl;
		}

		public static string GetBaseUrl(this UrlHelper urlHelper)
		{
			return SlamCmsUrlHelper.GetBaseUrl();
		}

		public static string GetBaseUrl(this ViewPage viewPage)
		{
			return SlamCmsUrlHelper.GetBaseUrl();
		}

		public static string GetBaseUrl(this ViewUserControl viewUserControl)
		{
			return SlamCmsUrlHelper.GetBaseUrl();
		}

		public static string GetBaseUrl(this WebViewPage webViewPage)
		{
			return SlamCmsUrlHelper.GetBaseUrl();
		}

		public static string ContentItemEdit(this UrlHelper urlHelper, ContentItem contentItem)
		{
			return urlHelper.ContentItemEdit(contentItem.ContentItemId);
		}

		public static string ContentItemEdit(this UrlHelper urlHelper, string contentItemId)
		{
			return urlHelper.AdminRedirector("EditForm", contentItemId);
		}

		public static string AdminRedirector(this UrlHelper urlHelper, string to, string slamId = null, object routeValues = null, string returnUrl = null)
		{
			return SlamCmsUrlHelper.AdminRedirector(to, slamId, routeValues, returnUrl);
		}

		public static string AdminRedirectorRelative(this UrlHelper urlHelper, string to, string slamId = null, object routeValues = null, string returnUrl = null)
		{
			return SlamCmsUrlHelper.AdminRedirectorRelative(to, slamId, routeValues, returnUrl);
		}

		public static string ContentItemDownload(this UrlHelper urlHelper, ContentItem contentItem, string slug = null)
		{
			return urlHelper.ContentItemDownload(contentItem.PrimaryContentItemId, slug ?? contentItem.Slug);
		}

		public static string ContentItemDownload(this UrlHelper urlHelper, string contentItemId, string slug = null)
		{
			return urlHelper.RouteUrl("ContentItemDownload", new { contentItemId, slug });
		}

		public static string SharePointImage(this UrlHelper urlHelper, string url, int width = 0, int height = 0, bool crop = false, int quality = 95)
		{
			var sharepointImageService = DependencyResolver.Current.GetService<SharePointImageService>();
			return sharepointImageService.RetrieveUrl(url, width, height, crop, quality);
		}

		public static string SharePointDocument(this UrlHelper urlHelper, string url)
		{
			var sharepointDocuemntService = DependencyResolver.Current.GetService<SharePointDocumentService>();
			return sharepointDocuemntService.RetrieveUrl(url);
		}

		public static string Image(this UrlHelper urlHelper, string fileName)
		{
			return SlamCmsUrlHelper.GetImageUrl(fileName);
		}

		public static string GetCurrentUrl(this UrlHelper urlHelper, object additionalRouteValues = null)
		{
			var currentUrl = urlHelper.RequestContext.HttpContext.Request.RawUrl;

			if (additionalRouteValues == null)
				return currentUrl;

			var routeValues = new RouteValueDictionary(additionalRouteValues);

			var sb = new StringBuilder();
			foreach (var routeValue in routeValues)
			{
				if (routeValue.Value != null)
					sb.Append("&{0}={1}".F(routeValue.Key, routeValue.Value));
			}

			sb.Remove(0, 1);

			currentUrl += (!currentUrl.Contains("?") ? "?" : "&") + sb;

			return currentUrl;
		}
	}
}
